//
//  TenViewController.m
//  UI_元旦豆瓣小集合
//
//  Created by dllo on 16/1/7.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "TenViewController.h"
#import "TenModel.h"
#import "UIImageView+WebCache.h"
#import "SDImageCache.h"
#import "TenCollectionViewCell.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface TenViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate>
@property (nonatomic, retain) UIScrollView *ScrollView;
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *arrAll;
@property (nonatomic, retain) NSMutableArray *arrAy;


@end

@implementation TenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"影集";
    self.navigationController.navigationBar.translucent = NO;
    
    [self handHandler];

    [self createCollectionView];
    [self scroll];

}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _arrAll.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TenCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    TenModel *model = _arrAy[indexPath.row];
    
    cell.model = model;
    
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"%ld", indexPath.row);
    
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
   
        
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
            // 设置头饰图
            UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
            headerView.backgroundColor = [UIColor purpleColor];
            headerView.alpha = 1;
            [headerView addSubview:_ScrollView];
            return  headerView;
            
            
        }else  {
            UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footer" forIndexPath:indexPath];
            footerView.backgroundColor = [UIColor blueColor];
            footerView.alpha = 0.5;
            return footerView;
        }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//layout
- (void) createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake(self.view.frame.size.width / 4, 170 );
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    layout.minimumLineSpacing = 50;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 375, self.view.frame.size.height - 113) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    
   
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView release];
    [self.view addSubview:_collectionView];
    [_collectionView registerClass:[TenCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
   [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
   [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
    layout.headerReferenceSize = CGSizeMake(100, 200);
//    layout.footerReferenceSize = CGSizeMake(100, 50);
    
}
- (void) scroll {
    self.ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    _ScrollView.contentSize = CGSizeMake(WIDTH * 27, 0);
    _ScrollView.pagingEnabled = YES;
//    _ScrollView.bounces = NO;
    _ScrollView.showsHorizontalScrollIndicator = NO;
    _ScrollView.contentOffset = CGPointMake(WIDTH , 0);
    _ScrollView.delegate = self;
    for (NSInteger i = 1; i < 27; i++) {
        TenModel *model = _arrAy[i - 1];
       
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i , 0, WIDTH, 200)];
    NSDictionary *dic = model.images;
        NSLog(@"%@", model.images);
        
    [image sd_setImageWithURL:[NSURL URLWithString:dic[@"large"]]];
    
    [_ScrollView addSubview:image];
   
    [image release];
        
    }
       
    
    
}

//数据解析
- (void)handHandler {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
    
   self.arrAll = [NSMutableArray arrayWithContentsOfFile:path];
   
    self.arrAy = [NSMutableArray array];

    for (NSDictionary *dic in _arrAll) {
        TenModel *model = [TenModel tenmodel];
        [model setValuesForKeysWithDictionary:dic];
        [_arrAy addObject:model];

}
   
}







@end
